package com.scs.application.modules.finance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.core.query.QueryGenerator;
import com.scs.application.modules.finance.dto.BillSettAllDTO;
import com.scs.application.modules.finance.dto.BillSettlementDTO;
import com.scs.application.modules.finance.entity.BillItem;
import com.scs.application.modules.finance.service.BillItemService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/finance/bill_item")
public class BillItemController extends CrudController<BillItemService, BillItem>{

    /**
     * 保存记帐单和结算单关系
     * @param billSettlementDTO
     * @return
     */
    @PostMapping("/saveSettlement")
    public RestResult saveSettlement(@RequestBody @Valid BillSettlementDTO billSettlementDTO) {
        service.saveSettlement(billSettlementDTO);
        return RestResult.ok();
    }
    /**
     * 一次保存所有记帐单和结算单关系
     * @param billSettAllDTO
     * @return
     */
    @PostMapping("/saveAllSettlement")
    public RestResult saveAllSettlement(@RequestBody @Valid BillSettAllDTO billSettAllDTO) {

        QueryContext queryContext =  new QueryContext();
        queryContext.setWrapper(QueryGenerator.initQueryWrapper(billSettAllDTO.getQueryString(),null));

        service.saveAllSettlement(queryContext,billSettAllDTO);
        return RestResult.ok();
    }
    /**
     * 删除选中记账单明细
     * @param billSettlementDTO
     * @return
     */
    @PostMapping("/deleteBillAllSettlement")
    public RestResult deleteBillAllSettlement(@RequestBody @Valid BillSettlementDTO billSettlementDTO) {
        service.deleteBillAllSettlement(billSettlementDTO);
        return RestResult.ok();
    }

    /**
     * @Description：消耗清单专用  page
     */
    @RequestMapping("/billInPage")
    public PageResult billInPage(@RequestBody Map<String, Object> reqData, HttpServletRequest request) {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Map<String, Object> userParam =null;
        //固定参数
        if(reqData.containsKey("userParam")){
            JSONObject ob = JSON.parseObject(reqData.get("userParam")+"");
            userParam= Maps.newHashMap(ob);
            queryContext.setParams(userParam);
        }
        IPage pageResult = service.billInPage(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }

    /**
    * @Description：消耗清单专用  list
    */
    @RequestMapping("/billInList")
    public RestResult billInList(@RequestBody Map<String, Object> reqData, HttpServletRequest request) {
        QueryContext queryContext = QueryContextHolder.currentContext();
        Map<String, Object> userParam =null;
        //固定参数
        if(reqData.containsKey("userParam")){
            JSONObject ob = JSON.parseObject(reqData.get("userParam")+"");
            userParam= Maps.newHashMap(ob);
            queryContext.setParams(userParam);
        }
        return RestResult.ok("查询成功", service.billInList(queryContext));
    }
}
